package com.runen.infinitemovement.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.SystemNotificationAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.MyDynamicListEntity;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SystemNotificationActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.rv_system)
    RecyclerView rvSystem;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    private SystemNotificationAdapter systemNotificationAdapter;
    private int page = 2;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_system_notification;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("系统通知");
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        rvSystem.setLayoutManager(layoutManager);
        systemNotificationAdapter = new SystemNotificationAdapter(this);
        systemNotificationAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvSystem.setAdapter(systemNotificationAdapter);

        initData();
        iniLoadMore();
    }

    private void iniLoadMore() {
        systemNotificationAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreRequested();
            }
        });
        systemNotificationAdapter.getLoadMoreModule().setAutoLoadMore(true);
        systemNotificationAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }

    private void initData() {
        srl.setRefreshing(true);
        String string = "?limit=20&page=1";
        new UserAPI().onSystemList(string, new BaseResultCallback<MyDynamicListEntity>() {
            @Override
            public void onResponse(MyDynamicListEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        systemNotificationAdapter.setNewData(response.data);
                        if (systemNotificationAdapter.getData().size() >= response.count) {
                            systemNotificationAdapter.getLoadMoreModule().loadMoreEnd(true);
                            systemNotificationAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        }
                    }
                }
            }
        });
    }

    @OnClick(R.id.top_btn_left)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }

    public void loadMoreRequested() {
        String string = "?limit=20&page=%s";
        new UserAPI().onSystemList(String.format(string, page), new BaseResultCallback<MyDynamicListEntity>() {
            @Override
            public void onResponse(MyDynamicListEntity response) {
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        systemNotificationAdapter.addData(response.data);
                        if (systemNotificationAdapter.getData().size() >= response.count) {
                            systemNotificationAdapter.getLoadMoreModule().loadMoreEnd(true);
                            systemNotificationAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        } else {
                            page++;
                            systemNotificationAdapter.getLoadMoreModule().loadMoreComplete();
                            systemNotificationAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        }
                    }
                }
            }
        });
    }

}
